package leave.dataaccess.mysql;

import leave.dataaccess.*;

import java.sql.*;

import leave.exception.*;

public class DAOFactoryImpl extends DAOFactory {

	private String connectionSource = "jdbc:mysql://localhost:3306/leavedatabase";
	private String connectionUserName = "javauser";
	private String connectionPassword = "password";

	private Connection FactoryConnection;
	
	public DAOFactoryImpl() throws DataAccessException{
		try{
			
			Class.forName("com.mysql.jdbc.Driver");
		}catch(Exception e){
			throw new DataAccessException("Unable to load Database driver.");
		}
		
		try{
			FactoryConnection = DriverManager.getConnection(connectionSource, connectionUserName, connectionPassword);
			FactoryConnection.setAutoCommit(false);
		}
		catch(SQLException e){
			throw new DataAccessException("Unable to initialize Database connection.");
		}
	}
	
	@Override
	public EmployeeLeaveDAO getEmployeeLeaveDAO() {
		// TODO Auto-generated method stub
		return new EmployeeLeaveDAOImpl(FactoryConnection);
	}

	@Override
	public PublicHolidayDAO getPublicHolidayDAO() {
		// TODO Auto-generated method stub
		return new PublicHolidayDAOImpl(FactoryConnection);
	}

	@Override
	public LeaveCountDAO getLeaveCountDAO() {
		// TODO Auto-generated method stub
		return new LeaveCountDAOImpl(FactoryConnection);
	}

	@Override
	public LeaveDAO getLeaveDAO() {
		// TODO Auto-generated method stub
		return new LeaveDAOImpl(FactoryConnection);
	}

	@Override
	public CompensationLeaveLedgerDAO getLedgerDAO() {
		// TODO Auto-generated method stub
		return new CompensationLeaveLedgerDAOImpl(FactoryConnection);
	}

	@Override
	public AdminDAO getAdminDAO() {
		// TODO Auto-generated method stub
		return new AdminDAOImpl(FactoryConnection);
	}
	
	@Override
	public LeaveTypeDAO getLeaveTypeDAO() {
		// TODO Auto-generated method stub
		return new LeaveTypeDAOImpl(FactoryConnection);
	}
	
	@Override
	public void commit() throws DataAccessException {
		// TODO Auto-generated method stub
		try {
			FactoryConnection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new DataAccessException("Unable to commit transaction.");
		}
		finally{
			try {
				FactoryConnection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new DataAccessException("Unable to close connection.");
			}
		}
	}
}
